import * as bookAlbumModel from '../../models/book_album';
import * as bookContentModel from '../../models/book_content';
import * as bookCategoryModel from '../../models/book_category';
import * as bookCategoryRelationModel from '../../models/book_category_relation';
import * as userBuyModel from '../../models/user_buy_book';
import * as validHelp from '../../helps/valid';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* searchList({ search, page, number }) {
  if (!validHelp.Required(search)) throw ValidException('搜索词为空');
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['title', 'like', `${search}`]);
  let result = yield bookAlbumModel.search(where, page, number);
  return result;
}

export function* getCategoryList({ page, number }) {
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['status', '=', 1]);
  let result = yield bookCategoryModel.search(where, page, number, 'order');
  return result;
}

export function* getCategory({ category_id, page, number }) {
  if (!validHelp.Integer(category_id)) throw ValidException('分类id为空');
  let where = [];
  where.push(['category_id', '=', category_id]);
  let result = yield bookCategoryRelationModel.search(where, page, number);
  let ids = result.list.map(row => row.album_id);
  let album_list = yield bookAlbumModel.getIds(ids);
  return {
    count: result.count,
    list: album_list,
  };
}

export function* getAlbum({ album_id }) {
  if (!validHelp.Integer(album_id)) throw ValidException('参数为空');
  let album = yield bookAlbumModel.get(album_id);
  if (!album) throw EmptyException('绘本不存在');
  let list = yield bookContentModel.allByAlbum(album_id, 'page', 'asc');
  album.list = list;
  return album;
}

export function* getScanAlbumList({ robot_id, page, number }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器未绑定');
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['robot_id', '=', '']);
  let result = yield userBuyModel.search(where, page, number, 'order');
  return result;
}

export function* addScanAlbum({ user_id, robot_id, number }) {
  if (!validHelp.Required(user_id)) throw ValidException('用户信息错误');
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(number)) throw ValidException('书号错误');
  let album = yield bookAlbumModel.getByNumber(number);
  if (!album) throw EmptyException('绘本不存在');
  let relation = yield userBuyModel.getByRobotAndAlbum(robot_id, album.id);
  relation = yield userBuyModel.add({ robot_id, user_id, album_id: album.id });
  if (!relation) throw SystemException('添加购买绘本失败');
  return {};
}

export function* deleteScanAlbum({ robot_id, album_id }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(album_id)) throw ValidException('绘本信息错误');
  let relation = yield userBuyModel.getByRobotAndAlbum(robot_id, album_id);
  if (!relation) throw EmptyException('未添加绘本');
  yield userBuyModel.del(relation.id);
  return {};
}
